package com.hanbang.gycatering.property;

import org.json.JSONException;
import org.json.JSONObject;

import com.hanbang.gangyu.R;
import com.hanbang.gycatering.aboutme.AddressManage;
import com.hanbang.gycatering.application.MyApplication;
import com.hanbang.gycatering.homepage.BaseActivity;
import com.hanbang.gycatering.myviews.NumberSelectView;
import com.hanbang.gycatering.myviews.NumberSelectView.OnItemClickListener;
import com.hanbang.gycatering.utils.DbUtil;
import com.hanbang.gycatering.utils.HttpInterfaces;
import com.hanbang.gycatering.utils.MyLog;
import com.hanbang.gycatering.utils.MyRequestCallBack;
import com.hanbang.gycatering.utils.MyToast;
import com.lidroid.xutils.exception.DbException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class WaitPayPropertyFee extends BaseActivity implements OnClickListener {
	private TextView   total_propertyfee,sumbit_propertyfee,
	 community_name;
	private EditText property_fee_edit;
	private Double total_money = 0.00;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.waitpay_propertyfee);
		initView();
		setTheListener();
	}
	private void initView() {
		// TODO Auto-generated method stub
		((TextView) findViewById(R.id.top_bar_title)).setText("缴纳物业费");
		community_name = (TextView) findViewById(R.id.community_name);
		sumbit_propertyfee = (TextView) findViewById(R.id.sumbit_propertyfee);
		property_fee_edit = (EditText) findViewById(R.id.property_fee_edit);
		total_propertyfee = (TextView) findViewById(R.id.total_propertyfee);
		community_name.setText(userData.getPlotname());
	}
	private void setTheListener() {
		// TODO Auto-generated method stub
		setBackArrows((ImageView) findViewById(R.id.top_bar_left_image));
		sumbit_propertyfee.setOnClickListener(this);
		property_fee_edit.addTextChangedListener(new TextWatcher() {
			private boolean isChanged = false;    
			@Override
			public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
				// TODO Auto-generated method stub
				total_propertyfee.setText("物业费：" + property_fee_edit.getText().toString() + "元");
			}
			
			@Override
			public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
					int arg3) {
				// TODO Auto-generated method stub
			}
			
			@Override
			public void afterTextChanged(Editable arg0) {
				// TODO Auto-generated method stub
				 if (isChanged) {// ----->如果字符未改变则返回    
	                    return;    
	                }    
	                String str = arg0.toString();    
	    
	                isChanged = true;    
	                String cuttedStr = str;    
	                /* 删除字符串中的dot */    
	                for (int i = str.length() - 1; i >= 0; i--) {    
	                    char c = str.charAt(i);    
	                    if ('.' == c) {    
	                        cuttedStr = str.substring(0, i) + str.substring(i + 1);    
	                        break;    
	                    }    
	                }    
	                /* 删除前面多余的0 */    
	                int NUM = cuttedStr.length();   
	                int zeroIndex = -1;  
	                for (int i = 0; i < NUM - 2; i++) {    
	                    char c = cuttedStr.charAt(i);    
	                    if (c != '0') {    
	                        zeroIndex = i;  
	                        break;  
	                    }else if(i == NUM - 3){  
	                        zeroIndex = i;  
	                        break;  
	                    }  
	                }    
	                if(zeroIndex != -1){  
	                    cuttedStr = cuttedStr.substring(zeroIndex);  
	                }  
	                /* 不足3位补0 */    
	                if (cuttedStr.length() < 3) {    
	                    cuttedStr = "0" + cuttedStr;    
	                }    
	                /* 加上dot，以显示小数点后两位 */    
	                cuttedStr = cuttedStr.substring(0, cuttedStr.length() - 2)    
	                        + "." + cuttedStr.substring(cuttedStr.length() - 2);    
	                
	                property_fee_edit.setText(cuttedStr);    
	    
	                property_fee_edit.setSelection(property_fee_edit.length());    
	                isChanged = false;    
	            
			}
		});
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.sumbit_propertyfee:
			if(property_fee_edit.getText().length()>0){
				MyLog.e("sgsg", property_fee_edit.getText().toString());
				if(Double.parseDouble(property_fee_edit.getText().toString())
						<(Double.parseDouble(userData.getBalanc()))){
					payThePropertyFee();
				}else{
					MyToast.show(WaitPayPropertyFee.this, "您的余额不足",
							Toast.LENGTH_SHORT);
				}
				
			}else{
				MyToast.show(WaitPayPropertyFee.this, "请输入缴费金额",
						Toast.LENGTH_SHORT);
			}
			break;
 
		default:
			break;
		}
	}

	private void payThePropertyFee() {
		// TODO Auto-generated method stub
		((MyApplication) getApplicationContext()).httpUtils.send(
				HttpMethod.GET, HttpInterfaces.addproperty + "&userphone="
						+ userData.getUserphone() + "&password="
						+ userData.getUserpassword()+"&money="+property_fee_edit.getText().toString(),
				new MyRequestCallBack<String>(WaitPayPropertyFee.this,
						getString(R.string.loadding)) {
					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {
						super.onSuccess(responseInfo);
						try {
							JSONObject json = new JSONObject(
									responseInfo.result);
							if (json.getInt("state") == 1) {
								if (userData != null) {
									try {
										userData.setBalanc(json.getString("blance"));
										DbUtil.getDbUtils(WaitPayPropertyFee.this).update(userData);
									} catch (DbException e) {
										e.printStackTrace();
									}
								}
								
								
								MyToast.show(WaitPayPropertyFee.this, "缴费成功 ",
										Toast.LENGTH_SHORT);
								 finish();
							} else {
								MyToast.show(WaitPayPropertyFee.this, "缴费失败",
										Toast.LENGTH_SHORT);
							}
						} catch (JSONException e) {
							e.printStackTrace();
							MyToast.show(WaitPayPropertyFee.this, "缴费失败",
									Toast.LENGTH_SHORT);
						}
					}
				});

	}
 
}
